/**
 * 0142
 *  
 * @Author : roryrywang
 * @DateTime : 2024/8/13 21:59
 */

#include <iostream>
#include <vector>

using namespace std;

struct ListNode {
    int val;
    ListNode *next;

    ListNode() : val(0), next(nullptr) {}

    ListNode(int x) : val(x), next(nullptr) {}

    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode *h = new ListNode(-1, head);
        ListNode *p = h;
        ListNode *s = h;

        while (s != nullptr && s->next != nullptr) {
            p = p->next;
            s = s->next->next;
            if (s == p) break;
        }

        if (s == nullptr || s->next == nullptr) return nullptr;

        s = h;
        while (p != s) {
            p = p->next;
            s = s->next;
        }
        return p;
    }
};

int main() {

    return 0;
}